package dao

import (
	"gorm.io/gorm"
	"gowork/microSignalSMSSending/model"
	"time"
)

type SMSRecordDaoImf struct {

}

func (sMSRecordDaoImf *SMSRecordDaoImf) GetSMSRecordByCustomerId(customerId int64, allocateTime time.Time, sendingType int, tx *gorm.DB) (int64 , error) {
	var count int64
	err := tx.Model(&model.SMSRecord{}).Where("customer_id = ?", customerId).
		Where("created_at >= ?", allocateTime.Format("2006-01-02 15:04:05")).
		Where("sending_type = ?", sendingType).
		Count(&count).Error
	//tx.Raw("SELECT count(id) FROM s_m_s_record WHERE customer_id = ? and created_at >= ? and sending_type = ?", customerId, allocateTime.Format("2006-01-02 15:04:05"), sendingType).Scan(&count)

	if err != nil {
		return 0, err
	}

	return count, nil
}

func (sMSRecordDaoImf *SMSRecordDaoImf) InsertSMSRecord(id int64, customerId int64, tx *gorm.DB, opts ...string) (map[string]interface{} , error) {
	var  sr = model.SMSRecord{
		Id:                id,
		CustomerId:        customerId,
		CustomerName:      opts[4],
		Account: opts[5],
		Content:           opts[1] + " " + opts[2],
		SendingType:       2,
		CreatedAt:         time.Now(),
		UpdatedAt:         time.Now(),
	}

	err := tx.Create(&sr).Error
	if err != nil {
		return nil, err
	}

	return map[string]interface{}{"code":200, "messga":"成功"}, nil
}
